package com.cskaoyan.service.userservice;

import com.cskaoyan.bean.userbean.*;
import com.cskaoyan.mapper.usermapper.*;
import com.cskaoyan.vo.uservo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hsp
 * @date 2021/5/8 21:59
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    CollectMapper collectMapper;
    @Autowired
    FootprintMapper footprintMapper;
    @Autowired
    SearchHistoryMapper searchHistoryMapper;
    @Autowired
    FeedbackMapper feedbackMapper;
    @Override//admin/user/list?page=1&limit=20&sort=add_time&order=desc
    public UserVo query(Integer page, Integer limit,String username,String mobile, String sort, String order) {
        PageHelper.startPage(page,limit);

        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (username!=null)
            criteria.andUsernameLike("%"+username+"%");
        if (mobile!=null)
            criteria.andMobileLike("%"+mobile+"%");
        userExample.setOrderByClause(sort+" "+order);//-->order by add_time desc
        List<User> users = userMapper.selectByExample(userExample); //条件查询

        PageInfo<User> userPageInfo = new PageInfo<>(users);
        long total = userPageInfo.getSize();
        UserVo userVo = new UserVo();
        userVo.setItems(users);
        userVo.setTotal(total);
        return userVo;
    }

    /**
     * @author kd
     * @return UserInfo(avatarUrl,nickname)
     */
    @Override
    public Map<String, String> query(Integer userId) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andIdEqualTo(userId);
        List<User> users = userMapper.selectByExample(userExample);
        Map<String, String> userInfo = new HashMap<>();
        userInfo.put("avatarUrl",users.get(0).getAvatar());
        userInfo.put("nickname",users.get(0).getNickname());
        return userInfo;
    }

    @Override
    public CollectVo collectList(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page,limit);
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        if (userId!=null)
            criteria.andUserIdEqualTo(userId);
        if (valueId!=null)
            criteria.andValueIdEqualTo(valueId);
        collectExample.setOrderByClause(sort + " "+order);

        List<Collect> collects = collectMapper.selectByExample(collectExample);

        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        int total = collectPageInfo.getSize();
        CollectVo collectVo = new CollectVo();
        collectVo.setItems(collects);
        collectVo.setTotal(total);

        return collectVo;
    }

    @Override
    public FootPrintVo footprintList(Integer page, Integer limit, Integer userId, Integer goodsId, String sort, String order) {
        PageHelper.startPage(page,limit);
        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (userId!=null)
            criteria.andUserIdEqualTo(userId);
        if (goodsId!=null)
            criteria.andGoodsIdEqualTo(goodsId);
        footprintExample.setOrderByClause(sort +" " +order);
        List<Footprint> footprints = footprintMapper.selectByExample(footprintExample);
        PageInfo<Footprint> footprintPageInfo = new PageInfo<>(footprints);
        int total = footprintPageInfo.getSize();
        FootPrintVo footPrintVo = new FootPrintVo();
        footPrintVo.setItems(footprints);
        footPrintVo.setTotal(total);
        return footPrintVo;
    }

    @Override
    public SearchHistoryVo historyList(Integer page, Integer limit, Integer userId, String keyword, String sort, String order) {
        PageHelper.startPage(page,limit);
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (userId!=null)
            criteria.andUserIdEqualTo(userId);
        if (keyword!=null)
            criteria.andKeywordEqualTo(keyword);
        searchHistoryExample.setOrderByClause(sort+" "+order);
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        int total = searchHistoryPageInfo.getSize();
        SearchHistoryVo searchHistoryVo = new SearchHistoryVo();
        searchHistoryVo.setItems(searchHistories);
        searchHistoryVo.setTotal(total);
        return searchHistoryVo;
    }

    @Override
    public FeedbackVo feedbackList(Integer page, Integer limit, String username, Integer id, String sort, String order) {
        PageHelper.startPage(page,limit);
        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (username!=null)
            criteria.andUsernameLike("%"+username+"%");
        if (id!=null)
            criteria.andIdEqualTo(id);
        feedbackExample.setOrderByClause(sort+" "+order);
        List<Feedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);
        PageInfo<Feedback> feedbackPageInfo = new PageInfo<>(feedbacks);
        int size = feedbackPageInfo.getSize();

        FeedbackVo feedbackVo = new FeedbackVo();
        feedbackVo.setItems(feedbacks);
        feedbackVo.setTotal(size);
        return feedbackVo;
    }

    @Override
    public Integer getUserIdByUsername(String username) {

       Integer userId =  userMapper.selectUserIdByUsername(username);

        return userId;
    }

    @Override
    public String selectAvatarByUsername(String username) {

       String avatar= userMapper.selectAvatarByUsername(username);

        return avatar;
    }
}
